package com.lsy.on_line.zaixian.config;

import com.lsy.on_line.zaixian.interceptor.CorsInterceptor;
import com.lsy.on_line.zaixian.interceptor.LoginInterceptor;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.sql.SQLOutput;

/**
 * 拦截器设置
 *
 * 不需要权限访问url /api/v1/pub/
 * 需要权限访问url  /api/v1/pri/
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    /**
     * 注入拦截器组件
     * @return
     */
    @Bean
    LoginInterceptor loginInterceptor(){
        return new LoginInterceptor();
    }

    /**
     * 将CorsInterceptor对象
     * 以组件形式注入ioc容器
     * @return
     */
    @Bean
    CorsInterceptor corsInterceptor(){
      return new CorsInterceptor();
    };

    /**
     * 拦截接口注册表配置
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry){

        //拦截全部路径，此跨域在最前面，因为有一个拦截执行顺序的问题
        registry.addInterceptor(corsInterceptor()).addPathPatterns("/**");

        //配置拦截全部需要权限登录的url
        registry.addInterceptor(loginInterceptor()).addPathPatterns("/api/v1/pri/*/*/**")
                //配置放行那些url --用户注册,登录接口 ---注意斜杠,不加不识别
                .excludePathPatterns("/api/v1/pri/zaixian/user/login","/api/v1/pri/zaixian/user/register");
        WebMvcConfigurer.super.addInterceptors(registry);
    }


//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//
//        registry.addMapping("/**")//允许全部请求
//                .allowedOriginPatterns("http://26.234.93.125")//允许跨域的请求方路径
//                .allowedHeaders("Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method",
//                        "Access-Control-Request-Headers","Accept")
//                .exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials","http://26.234.93.45:8081")
//                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
//                .maxAge(3600)
//                .allowCredentials(true);
//    }

}
